package com.ilearn.iodata.iolingo.servlet;

import lombok.extern.slf4j.Slf4j;
import org.apache.olingo.server.api.ODataHttpHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * HTTP长连接实现
 *
 * @author 单红宇(365384722)
 * @myblog http://blog.csdn.net/catoop/
 * @create 2016年3月29日
 *
 * http://localhost:8081/blockservlet/$metadata
 *
 */
@Slf4j
@WebServlet(name = "blockservlet", urlPatterns = "/blockservlet/*")
//@ConditionalOnBean(com.ilearn.iodata.iolingo.config.OdataConfig.class)
//异步处理的servlet若存在过滤器，则过滤器的注解@WebFilter应设置asyncSupported=true，
//否则会报错A filter or servlet of the current chain does not support asynchronous operations.
public class BlockServlet extends HttpServlet {

    private static final long serialVersionUID = -8685285401859800066L;

    /*@Autowired
    private OdataConfig odataConfig;*/

    /*@Autowired
    private CsdlAbstractEdmProvider edmProvider;

    @Autowired
    private EntityCollectionProcessor enityCollectionProcessor;*/

    @Autowired
    private ODataHttpHandler oDataHttpHandler;

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {

            // create odata handler and configure it with EdmProvider and Processor
            // 创建odata处理程序并使用EdmProvider和Processor对其进行配置

           /* OData odata = OData.newInstance();

            ServiceMetadata edm = odata.createServiceMetadata(edmProvider,
                    new ArrayList<EdmxReference>());

            ODataHttpHandler oDataHttpHandler = odata.createHandler(edm);

            //ODataHandler handler = new ODataHandler(odata, edm);
            oDataHttpHandler.register(enityCollectionProcessor);*/
            System.out.println("<<<<<<<<<<"+ Thread.currentThread().getName());
            //oDataHttpHandler = odataConfig.getHandler();

            oDataHttpHandler.process(req, resp);

        } catch (RuntimeException e) {
            log.error("Server Error occurred in ExampleServlet", e);
            throw new ServletException(e);
        }

    }

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        ServletContext servletContext = servletConfig.getServletContext();
        WebApplicationContext webApplicationContext = WebApplicationContextUtils
                .getWebApplicationContext(servletContext);
        AutowireCapableBeanFactory autowireCapableBeanFactory = webApplicationContext
                .getAutowireCapableBeanFactory();
        autowireCapableBeanFactory.configureBean(this,"odataConfig");
    }
}